package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.BackDto;
import io.geekidea.boot.shop.entity.Back;
import io.geekidea.boot.shop.mapper.BackMapper;
import io.geekidea.boot.shop.query.app.AppBackQuery;
import io.geekidea.boot.shop.query.web.BackQuery;
import io.geekidea.boot.shop.service.base.api.BackService;
import io.geekidea.boot.shop.vo.app.AppBackVo;
import io.geekidea.boot.shop.vo.web.BackVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class BackServiceImpl extends ServiceImpl<BackMapper, Back> implements BackService {

    @Autowired
    private BackMapper backMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addBack(BackDto dto) {
        Back back = new Back();
        BeanUtils.copyProperties(dto, back);
        return save(back);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateBack(BackDto dto) {
        Integer id = dto.getId();
        Back back = getById(id);
        if (back == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, back);
        return updateById(back);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteBack(Integer id) {
        return removeById(id);
    }

    @Override
    public BackVo getBackById(Integer id) {
        return backMapper.getBackById(id);
    }

    @Override
    public Paging<BackVo> getBackPage(BackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<BackVo> list = backMapper.getBackPage(query);
        Paging<BackVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppBackVo getAppBackById(Integer id) {
        return backMapper.getAppBackById(id);
    }

    @Override
    public Paging<AppBackVo> getAppBackPage(AppBackQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppBackVo> list = backMapper.getAppBackPage(query);
        Paging<AppBackVo> paging = new Paging<>(list);
        return paging;
    }

}
